package com.diandian.backend.entrance.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.diandian.backend.entrance.client.DianDianDingDingExchangeClient;
import com.diandian.backend.entrance.client.dto.dingding.DingdingMessageSendParamDTO;
import com.diandian.backend.entrance.constants.EntranceConstant;
import com.diandian.backend.entrance.dto.ClientPermissionCacheDataDTO;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.BaseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RouterDingDingExchangeService {

    private static final Logger logger = LoggerFactory.getLogger(RouterDingDingExchangeService.class);

    @Autowired
    private DianDianDingDingExchangeClient dingDingExchangeClient;

    public Object execute(ClientPermissionCacheDataDTO clientInfoDTO, String clientKey, String clientSecret, String cmd, JSONObject data) throws Exception{

        // 根据部门ID获取部门详情
        if(EntranceConstant.ROUTER_DINGDING_EXCHANGE_DEPT_GETBYDEPTID.equals(cmd)){

            logger.info("执行根据部门ID获取部门详情请求，参数: {}", data.toJSONString());

            String appKey = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DINGDING, EntranceConstant.SETTING_TYPE_DINGDING_APPKEY);
            String appSecret = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DINGDING, EntranceConstant.SETTING_TYPE_DINGDING_APPSECRET);
            if(appKey == null || "".equals(appKey) || appSecret == null || "".equals(appSecret)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            Long dingDeptId = null;
            if(data != null ) {
                dingDeptId = data.getLong("dingDeptId");
            }

            // 执行请求
            Object resultDTO = dingDingExchangeClient.getByDeptId(appKey, appSecret, dingDeptId);

            logger.info("根据部门ID获取部门详情请求结果: {}", JSON.toJSONString(resultDTO));

            return resultDTO;
        }

        // 发送钉钉文本消息
        if(EntranceConstant.ROUTER_DINGDING_EXCHANGE_MESSAGE_SEND_TEXT.equals(cmd)){

            logger.info("发送钉钉文本消息请求，参数: {}", data.toJSONString());

            String appKey = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DINGDING, EntranceConstant.SETTING_TYPE_DINGDING_APPKEY);
            String appSecret = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DINGDING, EntranceConstant.SETTING_TYPE_DINGDING_APPSECRET);
            if(appKey == null || "".equals(appKey) || appSecret == null || "".equals(appSecret)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            if(data == null){
                throw new DefaultException("参数缺失");
            }
            DingdingMessageSendParamDTO dataDTO = JSONObject.parseObject(data.toString(), DingdingMessageSendParamDTO.class);

            // 执行请求
            BaseResult resultDTO = dingDingExchangeClient.sendText(appKey, appSecret, dataDTO);

            logger.info("发送钉钉文本消息请求结果: {}", JSON.toJSONString(resultDTO));

            return resultDTO;
        }

        // 获取token
        if(EntranceConstant.ROUTER_DINGDING_EXCHANGE_TOKEN_GET.equals(cmd)){

            String appKey = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DINGDING, EntranceConstant.SETTING_TYPE_DINGDING_APPKEY);
            String appSecret = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DINGDING, EntranceConstant.SETTING_TYPE_DINGDING_APPSECRET);
            if(appKey == null || "".equals(appKey) || appSecret == null || "".equals(appSecret)){
                throw new DefaultException("配置缺失");
            }

            // 执行请求
            BaseResult resultDTO = dingDingExchangeClient.getToken(appKey, appSecret);

            logger.info("获取token请求结果: {}", JSON.toJSONString(resultDTO));

            return resultDTO;
        }

        // 根据用户ID获取用户信息
        if(EntranceConstant.ROUTER_DINGDING_EXCHANGE_USER_GETUSERINFOBYUSERID.equals(cmd)){

            logger.info("根据用户ID获取用户信息请求，参数: {}", data.toJSONString());

            String appKey = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DINGDING, EntranceConstant.SETTING_TYPE_DINGDING_APPKEY);
            String appSecret = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DINGDING, EntranceConstant.SETTING_TYPE_DINGDING_APPSECRET);
            if(appKey == null || "".equals(appKey) || appSecret == null || "".equals(appSecret)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            String dingUserId = null;
            if(data != null ) {
                dingUserId = data.getString("dingUserId");
            }

            // 执行请求
            Object resultDTO = dingDingExchangeClient.getUserInfoByUserId(appKey, appSecret, dingUserId);

            logger.info("根据用户ID获取用户信息请求结果: {}", JSON.toJSONString(resultDTO));

            return resultDTO;
        }

        // 根据手机号获取用户信息
        if(EntranceConstant.ROUTER_DINGDING_EXCHANGE_USER_GETUSERINFOBYMOBILE.equals(cmd)){

            logger.info("根据手机号获取用户信息请求，参数: {}", data.toJSONString());

            String appKey = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DINGDING, EntranceConstant.SETTING_TYPE_DINGDING_APPKEY);
            String appSecret = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DINGDING, EntranceConstant.SETTING_TYPE_DINGDING_APPSECRET);
            if(appKey == null || "".equals(appKey) || appSecret == null || "".equals(appSecret)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            String mobile = null;
            if(data != null ) {
                mobile = data.getString("mobile");
            }

            // 执行请求
            Object resultDTO = dingDingExchangeClient.getByMobile(appKey, appSecret, mobile);

            logger.info("根根据手机号获取用户信息请求结果: {}", JSON.toJSONString(resultDTO));

            return resultDTO;
        }

        // 获取部门列表
        if(EntranceConstant.ROUTER_DINGDING_EXCHANGE_DEPT_GET_LIST.equals(cmd)){

            logger.info("获取部门列表请求，参数: {}", data.toJSONString());

            String appKey = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DINGDING, EntranceConstant.SETTING_TYPE_DINGDING_APPKEY);
            String appSecret = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DINGDING, EntranceConstant.SETTING_TYPE_DINGDING_APPSECRET);
            if(appKey == null || "".equals(appKey) || appSecret == null || "".equals(appSecret)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            Long dingDeptId = null;
            String language = "";
            if(data != null ) {
                dingDeptId = data.getLong("dingDeptId");
                language = data.getString("language");
            }

            // 执行请求
            Object resultDTO = dingDingExchangeClient.getDeptList(appKey, appSecret, dingDeptId, language);

            logger.info("获取部门列表结果: {}", JSON.toJSONString(resultDTO));

            return resultDTO;
        }

        // 获取部门用户详情(全部）
        if(EntranceConstant.ROUTER_DINGDING_EXCHANGE_DEPT_GET_ALL_USER_DETAIL.equals(cmd)){

            logger.info("获取部门用户详情(全部）请求，参数: {}", data.toJSONString());

            String appKey = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DINGDING, EntranceConstant.SETTING_TYPE_DINGDING_APPKEY);
            String appSecret = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_DINGDING, EntranceConstant.SETTING_TYPE_DINGDING_APPSECRET);
            if(appKey == null || "".equals(appKey) || appSecret == null || "".equals(appSecret)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            Long dingDeptId = null;
            String language = "";
            String orderField = "";
            Boolean containAccessLimit = false;
            if(data != null ) {
                dingDeptId = data.getLong("dingDeptId");
                language = data.getString("language");
                orderField = data.getString("orderField");
                containAccessLimit = data.getBoolean("containAccessLimit");
            }

            // 执行请求
            Object resultDTO = dingDingExchangeClient.getAllDeptUserDetail(appKey, appSecret, dingDeptId, orderField, containAccessLimit, language);

            logger.info("获取部门用户详情(全部）结果: {}", JSON.toJSONString(resultDTO));

            return resultDTO;
        }

        throw new DefaultException("暂不支持此操作");
    }

}
